const Start = {
    enemies: [], //所有的敌机
    bullets: [], //所有的子弹

    //注册事件监听
    addListener: function () {
        const that = this;
        //点击开始
        on(Map.openElement, "click", function () {
            this.style.display = "none";
            that.play();
        });
        //鼠标在游戏界面移动
        on(Map.startElement, "mousemove", function (e) {
            //坐标
            let x = e.pageX - this.offsetParent.offsetLeft,
                y = e.pageY - this.offsetParent.offsetTop;
            //战机移动
            Fighter.go(x, y);
        });
    },
    //组织游戏流程
    play: function () {
        let count = 0;
        //启动计时器，游戏开始
        const timer = setInterval(() => {
            count++;
            if (count % 10 === 0) {
                this.enemies.push(EnemyFactory.createEnemy("small"));
            }
            if (count % 40 === 0) {
                this.enemies.push(EnemyFactory.createEnemy("middle"));
            }
            if (count % 80 === 0) {
                this.enemies.push(EnemyFactory.createEnemy("big"));
            }
            if (count % 10 === 0) {
                //生成子弹
                const bullet = new Bullet({
                    width: 6,
                    height: 14,
                    x: Fighter.x + Fighter.width / 2 - 3,
                    y: Fighter.y - 14,
                    img: "images/bullet.png",
                    speed: -4
                });
                const bulleta = new Bullet({
                    width: 6,
                    height: 14,
                    x: Fighter.x + Fighter.width / 2 + 20,
                    y: Fighter.y - 14,
                    img: "images/bullet.png",
                    speed: -4
                });
                const bulletb = new Bullet({
                    width: 6,
                    height: 14,
                    x: Fighter.x + Fighter.width / 2 - 24,
                    y: Fighter.y - 14,
                    img: "images/bullet.png",
                    speed: -4
                });
                const bulletc = new Bullet({
                    width: 6,
                    height: 14,
                    x: Fighter.x + Fighter.width / 2 + 37,
                    y: Fighter.y - 14,
                    img: "images/bullet.png",
                    speed: -4
                });
                const bulletd = new Bullet({
                    width: 6,
                    height: 14,
                    x: Fighter.x + Fighter.width / 2 - 41,
                    y: Fighter.y - 14,
                    img: "images/bullet.png",
                    speed: -4
                });
                this.bullets.push(bullet)
                this.bullets.push(bulleta)
                this.bullets.push(bulletb)
                this.bullets.push(bulletc)
                this.bullets.push(bulletd)
            }
            //敌机移动
            this.enemies.forEach(curr => {
                curr.go();
            });
            //让子弹飞
            this.bullets.forEach(curr => {
                curr.go();
            });
            //判断敌人、子弹、自己是否碰撞
            for (let i = this.enemies.length - 1; i >= 0; i--) {
                const enemy = this.enemies[i];
                for (let j = this.bullets.length - 1; j >= 0; j--) {
                    const bullet = this.bullets[j];
                    //调用方法，判断
                    const b = this.check(enemy, bullet)
                    if (b == false) {
                        //碰撞，销毁资源
                        bullet.die();
                        enemy.die();
                        this.bullets.splice(j, 1);
                        this.enemies.splice(i, 1);
                        break;
                    }
                }
            }
            for (let i = this.enemies.length - 1; i >= 0; i--) {
                const enemy =
                    this.enemies[i];
                //判断是否与自己碰撞
                if (this.check(enemy, Fighter) == false) {
                    enemy.die();
                    this.enemies.splice(i, 1);
                    Fighter.die();
                    clearInterval(timer);
                    break;
                }
            }
        }, 1000 / 60)
    },
    //判断碰撞
    check: function (role1, role2) {
        return (role1.y > role2.y + role2.height || role1.y + role1.height < role2.y ||
            role1.x > role2.x + role2.width ||
            role1.x + role1.width < role2.x)
    },
    begin: function () {
        this.addListener();
    }
}
// 启动游戏
Start.begin();